package com.situ.fruit;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;


public class FruitMange {
	Scanner sc = new Scanner(System.in);
	FruitDao fd = new FruitDao();
	RecordDao rd = new RecordDao();
	public void start() {
		while (true) {
			System.out.println("------水果销售-----");
			System.out.println("1.进货");
			System.out.println("2.显示库存数");
			System.out.println("3.买水果");
			System.out.println("4.查询全部销售记录");
			System.out.println("5.查询指定日期的销售利润");
			System.out.println("6.显示水果的信息");
			System.out.println("7.退出");
			System.out.println("请输入：");
			switch (Integer.parseInt(sc.nextLine())) {
			case 1:
				stockFruit();
				break;
			case 2:
				showStock();
				break;
			case 3:
				buyFruit();
				break;
			case 4:
				findAllRecord();
				break;
			case 5:
				findRecordOfTime();
				break;
			case 6:
				showFruit();
				break;
			case 7:
				return;
			default:
				System.out.println("输入有误！！！");
				break;
			}
		}
	}

	/**
	 * 进货
	 */
	public void stockFruit() {
		while (true) {
			System.out.println("-----进货-----");
			System.out.println("请输入水果名:");
			String name = sc.nextLine();
			FruitModel fm = isExistFruit(name);
			if (fm != null) {
				System.out.println("已存在该水果，继续将只添加数量");
				System.out.println("输入进货数量:");
				int number = Integer.parseInt(sc.nextLine());
				fm.setStock(fm.getStock() + number);
				fm.setNumber(fm.getNumber() + number);
				if (fd.update(fm) != 0) {
					System.out.println("进货成功！！！");
				}
				if (fd.update(fm) == 0) {
					System.out.println("进货失败！！！");
				}
			} else {
				System.out.println("输入水果重量:");
				int weight = Integer.parseInt(sc.nextLine());
				System.out.println("输入水果单价:");
				float price = Float.parseFloat(sc.nextLine());
				System.out.println("输入进货数量:");
				int number = Integer.parseInt(sc.nextLine());
				FruitModel fm1 = new FruitModel();
				fm1.setName(name);
				fm1.setWeight(weight);
				fm1.setPrice(price);
				fm1.setStock(number);
				fm1.setNumber(number);
				if (fd.insert(fm1) != 0) {
					System.out.println("进货成功！！！");
				} else {
					System.out.println("进货失败！！！");
				}
			}

			System.out.println("是否继续进货(y/n):");
			if (sc.nextLine().equalsIgnoreCase("n"))
				break;
		}
	}

	/**
	 * 进货/买水果子功能 -- 判断水果是否存在
	 * 
	 * @param name
	 * @return
	 */
	public FruitModel isExistFruit(String name) {
		List<FruitModel> list = fd.selectList(name);
		for (FruitModel fm : list) {
			if (fm.getName().equals(name)) {
				return fm;
			}
		}
		return null;
	}

	/**
	 * 显示全部水果的库存数
	 */
	public void showStock() {
		List<FruitModel> list = fd.selectList(null);
		for (FruitModel fm : list) {
			System.out.println(" 水果种类:" + fm.getName() + " 库存:" + fm.getStock());
		}
	}

	/**
	 * 买水果
	 */
	public void buyFruit() {
		while (true) {
			System.out.println("-----购买水果-----");
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("HH");
			int hour = Integer.parseInt(sdf.format(date));
			if (hour > 17) {
				System.out.println("现在已经" + hour + "点了，全场五折特价！！！");
			}
			System.out.println("您是否为女性(y/n)");
			String sex = sc.nextLine();
			if (sex.equalsIgnoreCase("y")) {
				System.out.println("现在购买水果打八折！！！");
			}
			// 展示全部水果
			showAllFruit();
			System.out.println("请输入要买的水果名称:");
			String name = sc.nextLine();
			FruitModel fm = isExistFruit(name);
			if (fm.getStock() <= 0) {
				System.out.println("该水果没有了！");
				break;
			}
			int number;
			while (true) {
				System.out.println("请输入要购买的数量:");
				number = Integer.parseInt(sc.nextLine());
				if (number > fm.getStock()) {
					System.out.println("该水果库存不够！");
					System.out.println("请重新输入!!");
				} else {
					break;
				}
			}
			// 结算价格 ，五点之后五折，女性八折
			Float price = fm.getPrice() * number;
			if (hour > 17) {
				if(sex.equalsIgnoreCase("y")) {
					price = (float) ((price / 2) * 0.8);
				} else {
					price = (float) (price * 0.8);
				}
			} else {
				if (sex.equalsIgnoreCase("y")) {
					price = (float) ((price) * 0.8);
				} else {
					price = (float) (price * 0.8);
				}
			}

			System.out.println(" 价格为:" + price);
			fm.setCount(fm.getCount() + number);
			fm.setStock(fm.getStock() - number);
			if (fd.update(fm) != 0) {
				RecordModel rm = new RecordModel();
				rm.setName(name);
				rm.setNumber(number);
				rm.setCost(price);
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				rm.setTime(sdf1.format(date));
				if (rd.insert(rm) != 0) {
					System.out.println("记录添加成功！！");
				} else {
					System.out.println("记录添加失败！！");
				}
				System.out.println("购买成功！！！");
			} else {
				System.out.println("购买失败！！！");
			}

			System.out.println("是否继续(y/n):");
			if (sc.nextLine().equalsIgnoreCase("n"))
				break;
		}
	}

	/**
	 * 买水果子功能 -- 展示水果信息
	 */
	public void showAllFruit() {
		List<FruitModel> list = fd.selectList(null);
		for (FruitModel fm : list) {
			System.out.println(" 水果种类:" + fm.getName() + " 水果重量:" + fm.getWeight() + " 水果价格:" + fm.getPrice() + " 库存:"
					+ fm.getStock());
		}
	}

	/**
	 * 查询全部销售记录
	 */
	public void findAllRecord() {
		List<RecordModel> list = rd.selectList(new RecordModel());
		if (list == null) {
			System.out.println("无购买记录");
			return;
		}
		for (RecordModel rm : list) {
			System.out.println(rm.getId() + " 水果名:" + rm.getName() + " 购买数量:" + rm.getNumber() + " 花费:" + rm.getCost()
					+ " 购买时间:" + rm.getTime());
		}

	}

	/**
	 * 查询指定日期段的销售利润
	 */
	public void findRecordOfTime() {
		System.out.println("------查询销售利润------");
		System.out.println("按照时间查询格式(年-月-日 时:分:秒 可以不写时分秒)");
		System.out.println("请输入开始时间:");
		String start = sc.nextLine();
		System.out.println("请输入结束时间:");
		String end = sc.nextLine();
		List<RecordModel> list = rd.selectListOfTime(start, end);
		Float sum = (float) 0;
		if ( list == null) {
			System.out.println("未找到销售记录");
		}else {
			System.out.println(start + " 到 " + end + " 的销售记录: ");
			for (RecordModel rm : list) {
				sum += rm.getCost();
				System.out.println(rm.getId() + " 水果名:" + rm.getName() + " 购买数量:" + rm.getNumber() + " 花费:"
						+ rm.getCost() + " 购买时间:" + rm.getTime());
			}
			System.out.println(" 总利润为 :" + sum);
		}
	}

	/**
	 * 显示当前水果的信息---前三个，输入y可全部显示
	 */
	public void showFruit() {
		List<FruitModel> list = fd.selectFruitList();
		for (FruitModel fm : list) {
			System.out.println("种类:" + fm.getName() + " 重量:" + fm.getWeight() + " 单价:" + fm.getPrice() + " 库存:"
					+ fm.getStock() + " 销售数量:" + fm.getCount() + " 总进货数:" + fm.getNumber());
		}
		System.out.println();
		while (true) {
		System.out.println("输入y显示全部  n返回水果摊");
		String choice = sc.nextLine();
		if (choice.equalsIgnoreCase("y")) {
			List<FruitModel> list1 = fd.selectAllFruitList();
			for (FruitModel fm : list1) {
				System.out.println("种类:" + fm.getName() + " 重量:" + fm.getWeight() + " 单价:" + fm.getPrice() + " 库存:"
						+ fm.getStock() + " 销售数量:" + fm.getCount() + " 总进货数:" + fm.getNumber());
			}
		}
		if (choice.equalsIgnoreCase("n")) {
			break;
		}

	}
	}
}
